package cn.edu.mju.property.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;

/*
* JDBC工具类 使用Durid连接池
*/
public class JDBCUtil {

//    private static DataSource ds;
    private static DruidDataSource dataSource = null;
    private static void initDataSource() throws Exception{
        if(dataSource==null){
            dataSource = new DruidDataSource();
            dataSource.setUrl("jdbc:mysql://localhost:3306/property");
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUsername("root");
            dataSource.setPassword("123456");

            dataSource.setInitialSize(1);
            dataSource.setMinIdle(1);
            dataSource.setMaxActive(10);

            dataSource.setMaxWait(20000);
            dataSource.setTimeBetweenLogStatsMillis(20000);

            dataSource.setValidationQuery("select 'x'");
            dataSource.setTestWhileIdle(true);
            dataSource.setTestOnBorrow(true);
        }

    }

    public static Connection getConnect() throws Exception{
        Connection conn = null;
        initDataSource();
        conn = dataSource.getConnection();
        return conn;
    }

}
